<template>
    <div class="store-detail">
        <div style="position: sticky;top: 0;z-index:999; ">
            <nav-bar title="店铺详情"></nav-bar>

        </div>
        <div class="store-background-image">
            <img :src="storeInfo.img" />
        </div>
        <div class="store-info-box">
            <div>
                <div class="store-info">
                    <div class="store-image">
                        <img :src="storeInfo.img" />
                    </div>
                    <div class="store-right-info">
                        <div class="store-name">
                            {{ storeInfo.name }}

                            <van-button type="default" style="font-size:12px; margin-left:10px;" size="small"
                                @click="collection">收藏</van-button>
                        </div>
                        <div>
                            <div></div>
                            <div class="time">配送约{{ storeInfo.time }}分钟</div>
                        </div>
                    </div>
                </div>

                <div class="store-intro">
                    公告：{{ storeInfo.intro }}
                </div>

                <!-- <div class="store-activite-list">
                    <span v-for="item in storeInfo.activite_list" :key="item.id"
                        style="display:inline-block; margin-right:10px;">
                        <van-tag plain type="primary">{{ item.name }}</van-tag>
                    </span>

                </div> -->
            </div>

            <div class="store">
                <van-tabs v-model="active" color="rgb(25,137,250)">
                    <van-tab title="点菜">

                        <Menu :category-list="storeInfo.categoriesCommodityList" :cart-commodity-list="cartCommodityList"
                            @addCommodityList="addCommodityList"></Menu>

                    </van-tab>
                    <van-tab title="评价">
                        <Comment></Comment>
                    </van-tab>
                </van-tabs>
            </div>


        </div>

        <div class="cart-box">
            <Cart :commodity-list="cartCommodityList" :store-id="id" @removeCartCommodityList="removeCartCommodityList"></Cart>
        </div>

    </div>
</template>

<script>
import store from '@/api/store'
import NavBar from '@/components/navbar/NavBar.vue'
import Menu from '@/components/commodity_detail/Menu.vue'
import Comment from '@/components/comment/Comment.vue'
import Cart from '@/components/store/Cart.vue'
export default {
    components: {
        NavBar,
        Menu,
        Comment,
        Cart,
    },
    data() {
        return {
            storeInfo: null,
            active: '点菜',
            id: null,
            cartCommodityList: [],
        }
    },
    created() {
        this.id = this.$route.params.id
        
        this.getStoreInfo()
    },
    methods: {
        getStoreInfo() {
            let id = this.id
            store.getStoreInfo(id)
                .then(res => {
                    if (res.code == 200) {

                        this.storeInfo = res.data.store

                        console.log(this.storeInfo)
                    } else {

                    }
                })
        },
        addCommodityList(data) {
            let have = false
            for (let i = 0; i < this.cartCommodityList.length; i++) {

                let commodity = this.cartCommodityList[i]

                if (commodity.id == data.id) {
                    commodity.number += 1
                    have = true
                }
            }

            if (!have) {
                let commodity = {
                    id: data.id,
                    img: data.img,
                    price: data.price,
                    name: data.name,
                    number: 1,
                }

                this.cartCommodityList.push(commodity)
            }

        },
        removeCartCommodityList(data) {
            let index = -1
            for (let i = 0; i < this.cartCommodityList.length; i++) {

                if(this.cartCommodityList[i].id == data.id) index = i
            }

            this.cartCommodityList.splice(index, 1)
        },
        collection() {
            let id = this.id

            alert("收藏" + id)
        }
    }
}
</script>

<style>
.store-detail .van-tabs__wrap {
    position: sticky;
    top: 40px;
    z-index: 999;
}
</style>

<style scoped>
.store-detail {
    text-align: left;

}

.store-background-image {
    height: 200px;
}

.store-background-image img {
    height: 100%;
    width: 100%;
}

.store-image {
    width: 50px;
    height: 50px;
}

.store-image img {
    width: 100%;
    height: 100%;
    border-radius: 8px;
}

.store-info-box {


    position: relative;

    bottom: 50px;

}

.store-info {
    display: flex;
    width: 100%;
}

.store-info-box>div:first-child {
    width: 90%;

    margin: auto;
    padding: 10px;

    background-color: rgb(255, 255, 255);

    border-radius: 8px;
    border: 1px solid rgb(204, 204, 204);
}

.store-image {
    margin-right: 10px;
}

.store-right-info {
    flex: 1;
}

.store-name {
    display: flex;

    justify-content: space-between;

    align-items: center;

    font-size: 16px;
    font-weight: 1000;
}

.store-intro {
    color: gray;
    margin-top: 10px;
}

.store-activite-list {
    margin-top: 20px;
}

.store {
    margin-top: 10px;
}

.cart-box {
    position: fixed;

    width: 100%;

    bottom: 10px;
    left: 0px;
}
</style>